package com.education.wisdom.controller.mq.config;


import com.education.wisdom.controller.mq.RabbitMQProperties;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import java.util.HashMap;
import java.util.Map;

//@Configuration
public class RabbitMQConfig {

    //交换机
    private static final String EXCHANGE = "wisdom_exchange_message";

    //TODO : 普通队列常量
    //队列
    private static final String QUEUE = "wisdom_queue_message";//直播留言发送
    //routing key名称
    private static final String ROUTINGKEY = "wisdom_routingkey_message";//直播留言发送

    //TODO : 延时队列常量
    //延时队列
    private static final String DELAY_QUEUE = "wisdom_queue_delay";
    //延时routing key
    private static final String DELAY_ROUTINGKEY = "wisdom_routingkey_delay";

    //TODO : 设置死信队列
    //死信交换机
    private static final String DEAD_EXCHANGE = "wisdom_exchange_dead";
    //死信队列
    private static final String DEAD_QUEUE = "wisdom_queue_dead";
    //死信路由
    private static final String DEAD_ROUTINGKEY = "wisdom_routingkey_dead";


    @Autowired
    private RabbitMQProperties rabbitMQProperties;

    //创建连接
    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(rabbitMQProperties.getHost(),rabbitMQProperties.getPort());
        connectionFactory.setUsername(rabbitMQProperties.getUsername());
        connectionFactory.setPassword(rabbitMQProperties.getPassword());
        connectionFactory.setVirtualHost(rabbitMQProperties.getVirtual_host());
        connectionFactory.setPublisherConfirms(true);
        return connectionFactory;
    }

    //定义RabbitTemplate  必须是prototype类型
    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public RabbitTemplate rabbitTemplate() {
        RabbitTemplate template = new RabbitTemplate(connectionFactory());
        return template;
    }

    //交换机
    @Bean
    public DirectExchange defaultExchange() {
        return new DirectExchange(EXCHANGE);
    }
    //TODO : 普通队列Bean对象
    //队列
    @Bean
    public Queue queue() {
        return new Queue(QUEUE, true); //队列持久
    }

    @Bean
    public Binding binding() {
        return BindingBuilder.bind(queue()).to(defaultExchange()).with(ROUTINGKEY);
    }

    //TODO : 延时队列Bean对象
    @Bean
    public Queue delayQueue() {
        Map args = new HashMap();
        //过期后自动把消息转到死信队列
        args.put("x-dead-letter-exchange",DEAD_EXCHANGE);
        args.put("x-dead-letter-routing-key",DEAD_ROUTINGKEY);
        args.put("x-message-ttl",30 * 60 * 1000);//设置过期时间为30分钟
        /**
         * 1 : 队列名称
         * 2 : true 设置队列为持久队列 在服务器重启会保留该队列
         * 3 : flase 设置队列非独占队列
         * 4 : flase 服务器不适应队列不会被删除
         * 5 : 声明队列参数
         */
        return new Queue(DELAY_QUEUE, true,false,false,args);
    }

    @Bean
    public Binding delayBinding() {
        return BindingBuilder.bind(delayQueue()).to(defaultExchange()).with(DELAY_ROUTINGKEY);
    }

    //TODO : 死信
    @Bean
    public DirectExchange deadExchange() {
        return new DirectExchange(DEAD_EXCHANGE);
    }

    @Bean
    public Queue deadQueue() {
        return new Queue(DEAD_QUEUE, true); //队列持久
    }

    @Bean
    public Binding deadBinding() {
        return BindingBuilder.bind(deadQueue()).to(deadExchange()).with(DEAD_ROUTINGKEY);
    }

}
